/*
 * GroupServiceImpl.java
 *
 * Project: TECHWATCH - TESTING TECHNOLOGIES
 *
 * Copyright 2008 by ELCA Informatique SA
 * Av. de la Harpe 22-24, 1000 Lausanne 13
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of ELCA Informatique SA. ("Confidential Information"). You
 * shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license
 * agreement you entered into with ELCA.
 */

package ch.elca.training.service.impl;

import java.util.ArrayList;
import java.util.List;

import ch.elca.training.dao.EmployeeDao;
import ch.elca.training.dao.GroupDao;
import ch.elca.training.dom.Employee;
import ch.elca.training.dom.Group;
import ch.elca.training.dto.GroupDto;
import ch.elca.training.service.GroupService;

/**
 * Implementation of {@link GroupService}.
 * 
 * @author abc
 * 
 */
public class GroupServiceImpl implements GroupService {

	private EmployeeDao employeeDao;

	private GroupDao groupDao;

	/**
	 * {@inheritDoc}
	 */
	public List<GroupDto> getAllWithMembers() {
		// TODO: [ADVANCED1] Implements first approach for query all groups with
		// members here.
		GroupDto temDto = null;
		List<GroupDto> rsDtos = new ArrayList<GroupDto>();
		List<Group> groups = groupDao.getAll();
		for (Group g : groups) {
			temDto = new GroupDto();
			temDto.setGroup(g);
			List<Employee> temList = employeeDao
					.getAllEmployeeInTheSameGroup(g.getId());
			temDto.setMembers(temList);
			rsDtos.add(temDto);
		}
		return rsDtos;
	}

	public List<GroupDto> getAllWithMembers2() {
		// TODO: [ADVANCED2] Implements second approach for query all groups
		// with members here.
		GroupDto temDto = null;
		List<GroupDto> rsDtos = new ArrayList<GroupDto>();
		List<Group> groups = groupDao.getAll();
		List<String> listGroupId = new ArrayList<String>();
		for( Group g : groups)
			listGroupId.add(String.valueOf(g.getId()));
		List<Employee> employees = employeeDao.getAllEmployeeInTheListGroup(listGroupId);
		for( Group group: groups){
			temDto = new GroupDto();
			temDto.setGroup(group);
			List<Employee> employeesOfGroup = new ArrayList<Employee>();
			for(Employee e: employees)
				if(group.getId() ==e.getGroupId())
					employeesOfGroup.add(e);
			temDto.setMembers(employeesOfGroup);
			rsDtos.add(temDto);				
		}
		return rsDtos;
	}

	public void setEmployeeDao(EmployeeDao employeeDao) {
		this.employeeDao = employeeDao;
	}

	public EmployeeDao getEmployeeDao() {
		return employeeDao;
	}

	public void setGroupDao(GroupDao groupDao) {
		this.groupDao = groupDao;
	}

	public GroupDao getGroupDao() {
		return groupDao;
	}

}
